package com.pay.cloud.fragment;

import com.pay.cloud.BindState;
import com.pay.cloud.DataFactory;
import com.pay.cloud.DataStore;
import com.pay.cloud.StatusListener;
import com.pay.cloud.cpay.MyCloudPay;
import com.pay.cloud.cpay.UnbindDeviceRequest;

import static com.pay.cloud.fragment.TipFragment.TipType.WAIT;

public class ShopUnbindFragment extends TipVerticalFragment implements BindState {
    private StatusListener listener;

    public ShopUnbindFragment(StatusListener listener) {
        super(WAIT, "正在解绑设备");
        this.listener = listener;
    }

    @Override
    public void execute() throws Exception {
        MyCloudPay.getInstance(getActivity()).unbindDevice(new UnbindDeviceRequest());

        DataStore store = DataFactory.getDataStore();
        store.setOutMchId(null);
        store.setOutSubMchId(null);
        store.setCloudCashierId(null);
        store.setAuthenType(null);
        store.setAuthenKey(null);
        store.setOutShopId(null);
        store.setShopName(null);
        store.setDeviceId(null);
        store.setDeviceName(null);
        store.setStaffId(null);
        store.setStaffName(null);

        MyCloudPay.init(getActivity());

        onSuccess();
    }

    @Override
    public void onError(String msg) {
        speak("设备解绑失败");
        listener.onError(msg);
    }

    private void onSuccess() {
        speak("设备解绑成功");
        listener.onSuccess("设备解绑成功");
    }

    @Override
    public boolean useNetwork() {
        return true;
    }
}
